package sketch.shape.geom;

import java.awt.Graphics2D;

import sketch.shape.Shape;
import sketch.shape.geom.prim.Point;


public class Diamond extends Shape {

	private Point refPoint; // top point

	private double width; // left - right

	private double height; // top - down


	public Diamond() {

	}

	public Point getCenter() {
		return new Point(getRefX() + getWidth() * .5, getRefY() + getHeight() * .5);
	}

	public double getHeight() {
		return height;
	}

	public void setHeight(double height) {
		this.height = height;
	}

	public Point getRefPoint() {
		return refPoint;
	}

	public void setRefPoint(Point refPoint) {
		this.refPoint = refPoint.clone();
	}

	public double getRefX() {
		return getRefPoint().getX();
	}

	public double getRefY() {
		return getRefPoint().getY();
	}

	public double getWidth() {
		return width;
	}

	public void setWidth(double width) {
		this.width = width;
	}

	public double getDX() {
		return .5 * getWidth();
	}

	public double getDY() {
		return .5 * getHeight();
	}

	/*
	 * p1 - p4 takes the clockwise order starts from refpoint
	 */
	public Point getP1() {
		return new Point(getRefPoint());
	}

	public Point getP2() {
		return getRefPoint().add(getDX(), getDY());
	}

	public Point getP3() {
		return getRefPoint().add(0, getHeight());
	}

	public Point getP4() {
		return getRefPoint().add(-getDX(), getDY());
	}

	public void drawComponent(Graphics2D g2) {
		super.drawComponent(g2);
		g2.draw(new java.awt.Polygon(
				new int[] {(int)getRefX(),
						   (int)(getRefX() + getDX()),
						   (int)getRefX(),
						   (int)(getRefX() - getDX())},
				new int[] {(int)getRefY(),
						   (int)(getRefY() + getDY()),
						   (int)(getRefY() + getHeight()),
						   (int)(getRefY() + getDY())},
				4
		));
	}

	public java.awt.Shape getBound(double GAP) {
		GAP *= 2;
		return new java.awt.Polygon(
				new int[] {(int)getRefX(),
						   (int)(getRefX() + getDX() + GAP),
						   (int)getRefX(),
						   (int)(getRefX() - getDX() - GAP)},
				new int[] {(int)(getRefY() - GAP),
						   (int)(getRefY() + getDY()),
						   (int)(getRefY() + getHeight() + GAP),
						   (int)(getRefY() + getDY())},
				4
		);
	}

	public String[][] getFieldValues() {
		return new String[][] {
				{"RefPoint", getRefPoint().toString()},
				{"Width", Double.toString(getWidth())},
				{"Height", Double.toString(getHeight())}
		};
	}

	public Point getTop() {
		return getP1();
	}

	public Point getBottom() {
		return getP3();
	}

	public Point getLeft() {
		return getP4();
	}

	public Point getRight() {
		return getP2();
	}

	public Rectangle toRectangle() {
		return new Rectangle(getRefX() - getDX(), getRefY(), getWidth(), getHeight());
	}

}
